package com.example.administrator.zeycar.Util;

/**
 * Created by Administrator on 2016/10/17 0017.
 */

import com.example.administrator.zeycar.common.Constant;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 *封装一个 retrofit 集成 0kHttp3 的抽象基类
 */
public abstract class RetrofitUtils {

    private static Retrofit mRetrofit;
    private static OkHttpClient mOkHttpClient;
    /**
     * 获取 Retrofit 对象
     *
     * @return
     */
    protected static Retrofit getRetrofit() {

        if (null == mRetrofit) {

            if (null == mOkHttpClient) {
                mOkHttpClient = new OkHttpClient.Builder().build();
            }

            //Retrofit2 后使用 build 设计模式
            mRetrofit = new Retrofit.Builder()
                    //设置服务器路径
                    .baseUrl(Constant.API_SERVER + "/")
                    //添加转化库，默认是 Gson
                    .addConverterFactory(GsonConverterFactory.create())
                    //添加回调库，采用 RxJava
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    //设置使用 okhttp 网络请求
                    .client(mOkHttpClient)
                    .build();
        }

        return mRetrofit;
    }

}
